package com.uni.action;

import com.uni.entity.User;
import com.uni.service.Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * --------------project-------------
 *
 * @Author Unirithe  - Mr.Chao
 * - CSDN:
 * https://blog.csdn.net/Unirithe
 * - GitHub:
 * https://github.com/unirithe
 * @create 2021/12/19 12:44
 * ------------------------------------------
 */
@WebServlet(name = "UserUpdateServlet", urlPatterns = "/toUpdate")
public class UserUpdateAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("不支持GET方式的请求");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String phone = req.getParameter("phone");
        User user = ((User)session.getAttribute("userInfo")).clone();
        user.setPhone(phone);
        boolean flag = Service.user.updateUser(user);
        if(flag) session.setAttribute("userInfo", user);
        resp.getWriter().write(flag ? "true" : "false");
    }
}
